//
//	ReaderMainToolbar.m
//	Reader v2.5.4
//
//	Created by Julius Oklamcak on 2011-07-01.
//	Copyright © 2011-2012 Julius Oklamcak. All rights reserved.
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights to
//	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to
//	do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//	CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "ReaderConstants.h"
#import "ReaderMainToolbar.h"
#import "ReaderDocument.h"
#import "AppDelegate.h"

#import <MessageUI/MessageUI.h>

@implementation ReaderMainToolbar

#pragma mark Constants

#define BUTTON_X 8.0f
#define BUTTON_Y 8.0f
#define BUTTON_SPACE 8.0f
#define BUTTON_HEIGHT 30.0f

#define DONE_BUTTON_WIDTH 56.0f
#define THUMBS_BUTTON_WIDTH 40.0f
#define PRINT_BUTTON_WIDTH 40.0f
#define EMAIL_BUTTON_WIDTH 40.0f
#define MARK_BUTTON_WIDTH 40.0f
#define COMPARE_BUTTON_WIDTH 40.0f
#define MYDOCUMENTS_BUTTON_WIDTH 40.0f

#define TITLE_HEIGHT 28.0f

#pragma mark Properties

@synthesize delegate;
@synthesize showInMyDocuments;

#pragma mark ReaderMainToolbar instance methods

- (id)initWithFrame:(CGRect)frame
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	return [self initWithFrame:frame document:nil];
}

- (id)initWithFrame:(CGRect)frame document:(ReaderDocument *)object
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	assert(object != nil); // Check

	if ((self = [super initWithFrame:frame]))
	{
		CGFloat viewWidth = self.bounds.size.width;

		UIImage *imageH = [UIImage imageNamed:@"Reader-Button-H.png"];
		UIImage *imageN = [UIImage imageNamed:@"Reader-Button-N.png"];

		UIImage *buttonH = [imageH stretchableImageWithLeftCapWidth:5 topCapHeight:0];
		UIImage *buttonN = [imageN stretchableImageWithLeftCapWidth:5 topCapHeight:0];

		CGFloat titleX = BUTTON_X; CGFloat titleWidth = (viewWidth - (titleX + titleX));

		CGFloat leftButtonX = BUTTON_X; // Left button start X position

#if (READER_STANDALONE == FALSE) // Option

		UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];

		doneButton.frame = CGRectMake(leftButtonX, BUTTON_Y, DONE_BUTTON_WIDTH, BUTTON_HEIGHT);
		[doneButton setTitle:NSLocalizedStringFromTable(@"close", @"Documents", nil) forState:UIControlStateNormal];
		[doneButton setTitleColor:[UIColor colorWithWhite:0.0f alpha:1.0f] forState:UIControlStateNormal];
		[doneButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:1.0f] forState:UIControlStateHighlighted];
		[doneButton addTarget:self action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
		[doneButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
		[doneButton setBackgroundImage:buttonN forState:UIControlStateNormal];
		doneButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
		doneButton.autoresizingMask = UIViewAutoresizingNone;

		[self addSubview:doneButton];   leftButtonX += (DONE_BUTTON_WIDTH + BUTTON_SPACE);

		titleX += (DONE_BUTTON_WIDTH + BUTTON_SPACE); titleWidth -= (DONE_BUTTON_WIDTH + BUTTON_SPACE);
        
        UIButton *renameButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
		renameButton.frame = CGRectMake(leftButtonX, BUTTON_Y, THUMBS_BUTTON_WIDTH, BUTTON_HEIGHT);
		[renameButton setImage:[UIImage imageNamed:@"rename.png"] forState:UIControlStateNormal];
		[renameButton addTarget:self action:@selector(renameButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
		[renameButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
		[renameButton setBackgroundImage:buttonN forState:UIControlStateNormal];
		renameButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
		renameButton.autoresizingMask = UIViewAutoresizingNone;
        
		[self addSubview:renameButton];   leftButtonX += (THUMBS_BUTTON_WIDTH + BUTTON_SPACE);
        
		titleX += (THUMBS_BUTTON_WIDTH + BUTTON_SPACE); titleWidth -= (THUMBS_BUTTON_WIDTH + BUTTON_SPACE);
        
        

#endif // end of READER_STANDALONE Option

        if([object.myDoc.fileType isEqualToString:@"pdf"]){
		UIButton *thumbsButton = [UIButton buttonWithType:UIButtonTypeCustom];

		thumbsButton.frame = CGRectMake(leftButtonX, BUTTON_Y, THUMBS_BUTTON_WIDTH, BUTTON_HEIGHT);
		[thumbsButton setImage:[UIImage imageNamed:@"Reader-Thumbs.png"] forState:UIControlStateNormal];
		[thumbsButton addTarget:self action:@selector(thumbsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
		[thumbsButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
		[thumbsButton setBackgroundImage:buttonN forState:UIControlStateNormal];
		thumbsButton.autoresizingMask = UIViewAutoresizingNone;

		[self addSubview:thumbsButton];  

              }    
            
		titleX += (THUMBS_BUTTON_WIDTH + BUTTON_SPACE); titleWidth -= (THUMBS_BUTTON_WIDTH + BUTTON_SPACE);


		CGFloat rightButtonX = viewWidth; // Right button start X position


		rightButtonX -= (MARK_BUTTON_WIDTH + BUTTON_SPACE);
         
        
        UIButton *openinButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        
        openinButton.frame = CGRectMake(rightButtonX, BUTTON_Y, MARK_BUTTON_WIDTH, BUTTON_HEIGHT);
        
        [openinButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        [openinButton addTarget:self action:@selector(openinButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [openinButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
        [openinButton setBackgroundImage:buttonN forState:UIControlStateNormal];
        openinButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        
        



		[self addSubview:openinButton]; titleWidth -= (MARK_BUTTON_WIDTH + BUTTON_SPACE);

        
  
        
        
        
        
	

#if (READER_ENABLE_MAIL == TRUE) // Option

		if ([MFMailComposeViewController canSendMail] == YES) // Can email
		{
            rightButtonX -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
            
            UIButton *emailButton = [UIButton buttonWithType:UIButtonTypeCustom];
            
            emailButton.frame = CGRectMake(rightButtonX, BUTTON_Y, EMAIL_BUTTON_WIDTH, BUTTON_HEIGHT);
            [emailButton setImage:[UIImage imageNamed:@"Reader-Email"] forState:UIControlStateNormal];
            [emailButton addTarget:self action:@selector(emailButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [emailButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
            [emailButton setBackgroundImage:buttonN forState:UIControlStateNormal];
            emailButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
            
            [self addSubview:emailButton];
            
            
            titleWidth -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
			
		}
        
            rightButtonX -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
            
           
        UIButton *flagButton = [UIButton buttonWithType:UIButtonTypeCustom];

            flagButton.frame = CGRectMake(rightButtonX, BUTTON_Y, EMAIL_BUTTON_WIDTH, BUTTON_HEIGHT);
            [flagButton setImage:[UIImage imageNamed:@"Reader-Mark-N.png"] forState:UIControlStateNormal];
            [flagButton addTarget:self action:@selector(markButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [flagButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
            [flagButton setBackgroundImage:buttonN forState:UIControlStateNormal];
            flagButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
            
            [self addSubview:flagButton]; titleWidth -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
            
            markButton = [flagButton retain]; markButton.enabled = NO; markButton.tag = NSIntegerMin;
            
            markImageN = [[UIImage imageNamed:@"Reader-Mark-N.png"] retain]; // N image
            markImageY = [[UIImage imageNamed:@"Reader-Mark-Y.png"] retain]; // Y image

			
#endif // end of READER_ENABLE_MAIL Option


//		if (object.password == nil) // We can only print documents without passwords
//		{
//			Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
//
//			if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
//			{
//				rightButtonX -= (PRINT_BUTTON_WIDTH + BUTTON_SPACE);
//
//				UIButton *printButton = [UIButton buttonWithType:UIButtonTypeCustom];
//
//				printButton.frame = CGRectMake(rightButtonX, BUTTON_Y, PRINT_BUTTON_WIDTH, BUTTON_HEIGHT);
//				[printButton setImage:[UIImage imageNamed:@"Reader-Print.png"] forState:UIControlStateNormal];
//				[printButton addTarget:self action:@selector(printButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
//				[printButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
//				[printButton setBackgroundImage:buttonN forState:UIControlStateNormal];
//				printButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
//
//				[self addSubview:printButton]; titleWidth -= (PRINT_BUTTON_WIDTH + BUTTON_SPACE);
//			}
//		}

        
#if (READER_ENABLE_COMPARE == TRUE) // Option

            rightButtonX -= (COMPARE_BUTTON_WIDTH + BUTTON_SPACE);
            
            UIButton *compareButton = [UIButton buttonWithType:UIButtonTypeCustom];
            
            compareButton.frame = CGRectMake(rightButtonX, BUTTON_Y, COMPARE_BUTTON_WIDTH, BUTTON_HEIGHT);
            [compareButton setImage:[UIImage imageNamed:@"compare.png"] forState:UIControlStateNormal];
            [compareButton addTarget:self action:@selector(compareButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [compareButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
            [compareButton setBackgroundImage:buttonN forState:UIControlStateNormal];
            compareButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
            
            [self addSubview:compareButton]; titleWidth -= (MARK_BUTTON_WIDTH + BUTTON_SPACE);
        
#endif // end of READER_BOOKMARKS Option


		if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
		{
			CGRect titleRect = CGRectMake(titleX, BUTTON_Y, titleWidth, TITLE_HEIGHT);

			titleLabel = [[UILabel alloc] initWithFrame:titleRect];

			titleLabel.textAlignment = UITextAlignmentCenter;
			titleLabel.font = [UIFont systemFontOfSize:19.0f]; // 19 pt
			titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
			titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
			titleLabel.textColor = [UIColor colorWithWhite:0.0f alpha:1.0f];
			titleLabel.shadowColor = [UIColor colorWithWhite:0.65f alpha:1.0f];
			titleLabel.backgroundColor = [UIColor clearColor];
			titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
			titleLabel.adjustsFontSizeToFitWidth = YES;
			titleLabel.minimumFontSize = 14.0f;
            if(object.myDoc)
            {
                titleLabel.text = [NSString stringWithFormat:@"%@/%@", object.myDoc.myFolder.name, object.myDoc.title];
            }

			[self addSubview:titleLabel]; [titleLabel release];
		}
	}

	return self;
}

-(id)initWithFrame:(CGRect)frame document:(ReaderDocument *)object showInMyDocuments:(BOOL)isShowInMyDocuments
{
    self.showInMyDocuments = isShowInMyDocuments;
    return [self initWithFrame:frame document:object];
}

- (void)dealloc
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

    [titleLabel release], titleLabel = nil;

	[markButton release], markButton = nil;

	[markImageN release], markImageN = nil;
	[markImageY release], markImageY = nil;

	[super dealloc];
}

- (void)setTitleLabel:(NSString *)title;
{

    
    [titleLabel setText:title];

}


- (void)setBookmarkState:(BOOL)state
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif


	if (state != markButton.tag) // Only if different state
	{
		if (self.hidden == NO) // Only if toolbar is visible
		{
			UIImage *image = (state ? markImageY : markImageN);

			[markButton setImage:image forState:UIControlStateNormal];
		}

		markButton.tag = state; // Update bookmarked state tag
	}

	if (markButton.enabled == NO) markButton.enabled = YES;

}

- (void)updateBookmarkImage
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif


	if (markButton.tag != NSIntegerMin) // Valid tag
	{
		BOOL state = markButton.tag; // Bookmarked state

		UIImage *image = (state ? markImageY : markImageN);

		[markButton setImage:image forState:UIControlStateNormal];
	}

	if (markButton.enabled == NO) markButton.enabled = YES;

}

- (void)hideToolbar
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	if (self.hidden == NO)
	{
		[UIView animateWithDuration:0.25 delay:0.0
			options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
			animations:^(void)
			{
				self.alpha = 0.0f;
			}
			completion:^(BOOL finished)
			{
				self.hidden = YES;
			}
		];
	}
}

- (void)showToolbar
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	if (self.hidden == YES)
	{
		[self updateBookmarkImage]; // First

		[UIView animateWithDuration:0.25 delay:0.0
			options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
			animations:^(void)
			{
				self.hidden = NO;
				self.alpha = 1.0f;
			}
			completion:NULL
		];
	}
}

#pragma mark UIButton action methods

- (void)doneButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	[delegate tappedInToolbar:self doneButton:button];
}


- (void)renameButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif
    
	[delegate tappedInToolbar:self renameButton:button];
}

- (void)thumbsButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	[delegate tappedInToolbar:self thumbsButton:button];
}

- (void)printButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	[delegate tappedInToolbar:self printButton:button];
}

- (void)emailButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	[delegate tappedInToolbar:self emailButton:button];
}

- (void)openinButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif
    
	[delegate tappedInToolbar:self openinButton:button];
}


- (void)markButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif

	[delegate tappedInToolbar:self markButton:button];
}

- (void)compareButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif
    
	[delegate tappedInToolbar:self compareButton:button];
}

- (void)myDocumentsButtonTapped:(UIButton *)button
{
#ifdef DEBUGX
	NSLog(@"%s", __FUNCTION__);
#endif
    
	[delegate tappedInToolbar:self myDocumentsButton:button];
}

@end
